﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Licenta1.Expressions;

namespace Licenta1.Statements
{
    class CollectionStatement:Statement
    {
        string name;

        public CollectionStatement(Context con, Expression name)
            : base(con)
        {
            this.name = ((IdentifierExpression)name).Name;
        }

        public override void Execute()
        {
            foreach (UserCollection uc in context.userCollections)
                if (uc.NAME == name)
                {
                    Console.WriteLine("A duplicate name for collection already exists.");
                    return;
                }
            context.userCollections.Add(new UserCollection(name));
        }

        public string NAME
        {
            get { return name; }
        }
    }



    class UserCollection
    {

        List<object> l;
        string name;

        public UserCollection(string name)
        {
            l = new List<object>();
            this.name = name;
        }

        public string NAME
        {
            set { name = value; }
            get { return name; }
        }

        public List<object> COLLECTION
        {
            get { return l; }
        }

        public void Add(object obj)
        {
            l.Add(obj);
        }

        public void Remove(object obj)
        {
            foreach (object o in l)
            {
                
                if (((Expression)o).Value == ((Expression)obj).Value)
                {
                    l.RemoveAt(l.IndexOf(o));
                    return;
                }
            }
        }

        public object First()
        {
            if (l.Count != 0)
                return l[0];
            return null;
        }

        public object Last()
        {
            if (l.Count != 0)
                return l[l.Count - 1];
            return null;
        }

        public void Clear()
        {
            l.Clear();
        }

        public int Size()
        {
            return l.Count;
        }
    }

}
